package com.sq.partner.manager.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.IGatewayChannelInfoConfigDao;
import com.sq.partner.manager.dao.ITradCreditLmtDefDao;
import com.sq.partner.manager.dao.ITradCreditLmtTraceDao;
import com.sq.partner.manager.mybatis.model.GatewayChannelInfo;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.mybatis.model.TradCreditLmtDef;
import com.sq.partner.manager.mybatis.model.TradCreditLmtTrace;
import com.sq.partner.manager.service.ICreditLmtControlService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

/**
 * 
 * @author Kang Zhiqiang 
 *
 */
@Service
public class CreditLmtControlServiceImpl implements ICreditLmtControlService{

	@Resource
	private IGatewayChannelInfoConfigDao channelInfoConfigDao;
	@Resource
	private ITradCreditLmtDefDao creditLmtDefDao;
	@Resource
	private ITradCreditLmtTraceDao creditLmtTraceDao;
	
	@Override
	public List<GatewayChannelInfo> queryChannelInfoListByChannelInfo(
			GatewayChannelInfo channelInfo) throws Exception {
		List<GatewayChannelInfo> channelInfoList = channelInfoConfigDao.queryListByChannelInfo(channelInfo);
		return channelInfoList;
	}

	@Override
	public Page<Map> queryNotInCreditLmtDefChannelInfo(Page<Map> page)
			throws Exception {
		Page<Map> resultPage = channelInfoConfigDao.queryNotInCreditLmtDefChannelInfo(page);
		return resultPage;
	}

	@Override
	public Page queryCreditLmtDefByChannelName(Integer pageIndex, String channelName,GatewayUser user)
			throws Exception {
		Page creditLmtDefPage = creditLmtDefDao.queryByChannelName(pageIndex, channelName,user);
		return creditLmtDefPage;
	}

	@Override
	public void saveOrUpdateCreditLmtDef(BigDecimal alertLmt,
			BigDecimal terminalLmt, String limitNo, String channelCode,String selfOnly, String operator)
			throws Exception {
		TradCreditLmtDef creditLmtDef = new TradCreditLmtDef();
		creditLmtDef.setAlertLmt(alertLmt);
		creditLmtDef.setTerminalLmt(terminalLmt);
		creditLmtDef.setOperator(operator);
		creditLmtDef.setSelfOnly(selfOnly);
		if(StrUtil.isNotEmpty(limitNo)){
			creditLmtDef.setLimitNo(limitNo);
			creditLmtDef.setUpdateTime(new Timestamp(new Date().getTime()));
			creditLmtDefDao.update(creditLmtDef);
		}else{
			creditLmtDef.setStatus("1");
			creditLmtDef.setPartnerCode(channelCode);
			creditLmtDef.setCreatTime(new Timestamp(new Date().getTime()));
			creditLmtDef.setUpdateTime(creditLmtDef.getCreatTime());
			creditLmtDefDao.save(creditLmtDef);
		}
	}

	@Override
	public void delCreditLmtDefByCreditLmtDef(TradCreditLmtDef creditLmtDef)
			throws Exception {
		//伪删除
		creditLmtDef.setStatus("0");
		creditLmtDefDao.update(creditLmtDef);
		
		//删除 creditLmtDefDao.delByCreditLmtDef(creditLmtDef);
	}

	@Override
	public void saveAppendMoney(TradCreditLmtTrace creditLmtTrace)
			throws Exception {
		creditLmtTrace.setOtherNo("000000");
		creditLmtTrace.setOthernoType("99");
		creditLmtTrace.setAppendType("03");
		creditLmtTrace.setCreatTime(new Timestamp(new Date().getTime()));
		
		creditLmtTraceDao.saveAppendMoney(creditLmtTrace);
	}

	@Override
	public Page queryAppendDetailInfoByLimitNo(Integer pageIndex, TradCreditLmtTrace creditLmtTrace)
			throws Exception {
		return creditLmtTraceDao.queryAppendDetailInfoByLimitNo(pageIndex, creditLmtTrace);
	}

	@Override
	public List<TradCreditLmtDef> queryTradCreditLmtDefsByChannelCode(
			String channelCode) throws Exception {
		return creditLmtDefDao.queryTradCreditLmtDefsByChannelCode(channelCode);
	}

	@Override
	public List<TradCreditLmtDef> queryTradCreditLmtDefs(
			TradCreditLmtDef creditLmtDef) throws Exception {
		return creditLmtDefDao.queryTradCreditLmtDefs(creditLmtDef);
	}

}
